﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
using Utility;
using System.IO;

public partial class VehicleUI_Employee_EmployeeAMV : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["Type"] == null)
            { }
            else if (Request.QueryString["Type"].ToString() == "View")
            {
                LoadService();
                txt_JoinDate.Enabled = false;
                txt_EmployeeID.Enabled = false;
                txt_Name.Enabled = false;
                txt_LeaveDate.Enabled = false;
                chk_IsPrivate.Enabled = false;
                btn_Sure.Visible = false;
            }
            else if (Request.QueryString["Type"].ToString() == "Edit")
            {
                LoadService();
            }
        }
    }

    protected void LoadService()
    {
        string _id = Request.QueryString["ID"].ToString();
        Employee cs = new EmployeeBLL().GetModel(_id);
        if (cs != null)
        {
            txt_EmployeeID.Text = cs.EmployeeID;
            txt_JoinDate.Text = cs.JoinDate.ToString();
            txt_Name.Text = cs.Name.ToString();
            chk_IsPrivate.Checked = cs.IsPrivate;
            if (cs.LeaveDate == DateTime.Parse("1900-1-1"))
            {
                txt_LeaveDate.Text = "";
            }
            else
            {
                txt_LeaveDate.Text = cs.LeaveDate.ToString();
            }
        }
    }

    protected void btn_Sure_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["Type"] == null)
        {
            Employee cs = new Employee();

            cs.EmployeeID = txt_EmployeeID.Text.Trim();
            cs.JoinDate = DateTime.Parse(txt_JoinDate.Text.Trim());
            cs.Name = txt_Name.Text.Trim();
            if (txt_LeaveDate.Text.Trim() != string.Empty)
            {
                cs.LeaveDate = DateTime.Parse(txt_LeaveDate.Text.Trim());
            }
            else
            {
                cs.LeaveDate = DateTime.Parse("1900-1-1");
            }
            cs.OrganID = (int)Session["OrganID"];
            cs.InputBy = Session["UserID"].ToString();

            bool re = new EmployeeBLL().Add(cs);

            if (re)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "添加成功! " + msg, "EmployeeMgr.aspx");
            }
            else
            {

                UtilityService.Alert(this.Page, "添加失败! ");
            }
        }
        else if (Request.QueryString["Type"] == "Edit")
        {
            string _id = Request.QueryString["ID"].ToString();
            Employee cs = new EmployeeBLL().GetModel(_id);
            cs.EmployeeID = txt_EmployeeID.Text.Trim();
            cs.JoinDate = DateTime.Parse(txt_JoinDate.Text.Trim());
            cs.Name = txt_Name.Text.Trim();
            if (txt_LeaveDate.Text.Trim() != string.Empty)
            {
                cs.LeaveDate = DateTime.Parse(txt_LeaveDate.Text.Trim());
            }
            else
            {
                cs.LeaveDate = DateTime.Parse("1900-1-1");
            }

            bool re = new EmployeeBLL().Update(cs);

            if (re)
            {
                string msg = "";
                UtilityService.AlertAndRedirect(this.Page, "修改成功! " + msg, "EmployeeMgr.aspx");
            }
            else
            {
                UtilityService.Alert(this.Page, "修改失败! ");
            }
        }
    }

}
